package com.itheima.mapper.system;

import com.itheima.domain.system.Role;
import com.itheima.domain.system.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {


    @Select("select * from users")
    @Results(id = "userMap",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "email",property = "email"),
            @Result(column = "username",property = "username"),
            @Result(column = "PASSWORD",property = "password"),
            @Result(column = "phoneNum",property = "phoneNum"),
            @Result(column = "STATUS",property = "status"),
            @Result(javaType = List.class,column = "id",property = "roles",
            many = @Many(select = "com.itheima.mapper.system.RoleMapper.findRolesByUid"))
    })
    List<User> findAll();

    @Select("select * from users where username=#{username} and PASSWORD=#{password}")
    @ResultMap("userMap")
    User findByIdAndPwd(User user);

    @Insert("insert into users values(#{id},#{email},#{username},#{password},#{phoneNum},#{status})")
    void save(User user);

    @Delete("delete from users where id=#{id}")
    void delete(String id);

    @Select("select r.id from role as r where r.id in(select roleId from users_role where userId=#{id})")
    List<String> findRoleIdsByUid(String id);

    @Select("select * from users as u where u.id in(select userId from users_role where roleId=#{id})")
    List<User> findUserByRid(String id);

    //查找用户详细信息
    @Select("select * from users where id=#{id}")
    @ResultMap("userMap")
    User findByUid(String id);

    @Update("update users set PASSWORD=#{password} where id = #{id}")
    void updatePwd(User user);

    @Insert("insert into users_role values(#{uid},#{id})")
    void addRoleToUser(@Param("uid") String uid, @Param("id") String id);


    @Delete("delete from users_role where userId=#{userId}")
    void deleteRole(String userId);

    //模糊查询
    @Select("SELECT * FROM users WHERE username LIKE #{username}")
    List<User> selectLike(String username);

    //修改状态
    @Update("update users set STATUS=#{status} where id=#{id}")
    void updateMsg(User user);

}
